package com.floreantpos.print;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.floreantpos.config.PrintConfig;
import com.floreantpos.jreports.JReportPrintService;
import com.floreantpos.model.Ticket;

import foxtrot.Job;
import foxtrot.Worker;

public class PosPrintService {

  private static Log logger = LogFactory.getLog(PosPrintService.class);

  public static void printTicket(final Ticket ticket) throws Exception {
    Job job = new Job() {

      @Override
      public Object run() {
        if (PrintConfig.isPrintReceiptEnabled()) {
          try {
            if (PrintConfig.getReceiptPrinterType() == PrinterType.OS_PRINTER) {
              JReportPrintService.printTicket(ticket);
              return null;
            }
          } catch (Exception x) {
            logger.error("Erro ao imprimir o Pedido", x);
          } finally {

          }
        }
        return null;
      }
    };

    Worker.post(job);
  }

  public static void printToKitchen(final Ticket ticket) throws Exception {
    Job job = new Job() {

      @Override
      public Object run() {
        if (PrintConfig.isPrintKitchenEnabled()) {
          try {
            if (PrintConfig.getKitchenPrinterType() == PrinterType.OS_PRINTER) {
              JReportPrintService.printTicketToKitchen(ticket);
              return null;
            }
          } catch (Exception x) {
            logger.error("Erro ao imprimir pedido para Preparação", x);
          } finally {

          }
        }
        return null;
      }
    };

    Worker.post(job);
  }
}
